<template>
  <div class="register-teacher">
    <header class="register-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>教师注册页</span>
      </div>
      <div class="right">
        课程管理系统
      </div>
    </header>
    <main class="register-main">
      <div class="form-container" style="line-height: 100px;text-align:center;">

        <!--头像上传-->
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8081/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

        <!-- 教师注册信息 -->
        <el-form ref="form" :model="form" :rules="rules">

          <el-form-item label="教师姓名" prop="teacherName">
            <el-input v-model="form.teacherName" />
          </el-form-item>
          <el-form-item label="教职工号" prop="employeeNumber">
            <el-input v-model="form.employeeNumber" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" type="email" />
          </el-form-item>
          <el-form-item label="密码" prop="accountPassword">
            <el-input v-model="form.accountPassword" show-password />
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPassword">
            <el-input v-model="form.checkPassword" show-password />
          </el-form-item>
          <el-form-item>
            <div class="btn-container">
        <el-button @click="reset()">重置</el-button>
        <el-button type="primary" @click="register">注册</el-button>
      </div>
          </el-form-item>
        </el-form>
      </div>
    </main>
  </div>
</template>

<script>
import { teachersRegisterAPI } from '@/api/user'
export default {
  data() {
    //自定义的邮箱和手机验证规则
    let checkEmail = (rule, value, callback) => {//验证邮箱
      const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (this.form.email !== '' && !regEmail.test(this.form.email)) {
        callback(new Error('请输入有效的邮箱'));
      }
    };
    //确认密码
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.accountPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      imageUrl: '',
      form: {
        avatar: '',
        teacherName: '',
        employeeNumber: '',
        email: '',
        accountPassword: '',
        checkPassword: ''
      },
      // 输入规则
      rules: {
        teacherName: [
          { required: true, message: '教师姓名必须填写，如：张三', trigger: 'blur' },
          { min: 1, max: 18, required: true, message: '姓名必须1-18位', trigger: 'blur' }
        ],
        employeeNumber: [
          { required: true, message: '教职工号必须填写，如：0000001', trigger: 'blur' },
          { min: 3, max: 16, required: true, message: '教职工号必须3-16位', trigger: 'blur' },
          // { type: 'number', message: '教职工号必须为数字值' }
        ],
        email: [
          { required: true, message: '请输入邮箱，如：8888888@qq.com', trigger: 'blur' },
          { validator: checkEmail, trigger: "blur" }],
        accountPassword: [
          { required: true, message: '密码必须填写', trigger: 'blur' },
          { min: 3, max: 16, required: true, message: '密码必须3-16位', trigger: 'blur' },
        ],
        checkPassword: [
          { min: 3, max: 16, required: true, message: '确认密码必须3-16位', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {

  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.imageUrl = res.data;
      this.form.avatar = res.data
      // console.log('resUP: ',res)
      // console.log('fileUP: ',file)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG或PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isJPG && isLt2M;
    },
    // 重置方法
    reset() {
      this.form.avatar = ''
      this.form.teacherName = ''
      this.form.employeeNumber = ''
      this.form.email = ''
      this.form.accountPassword = ''
      this.form.checkPassword = ''
    },
    //注册方法
    register() {
      teachersRegisterAPI(this.form).then((res) => {
        // console.log(res);
        // console.log(this.form)
        if(res.data !== 1){
          this.$message.error('注册失败，请检查输入信息！')
          return
        }
        this.$message.success('注册成功！')
        this.reset()
        // 设置延迟返回登录页的时间
        const delay = 800;
        // 使用 setTimeout 来实现延迟跳转
        setTimeout(() => {
          this.$router.push('/login');
        }, delay);

      });
    }
  }
}
</script>

<style scoped lang="scss">
.register-teacher {
  background-color: #f4f6f8;
  height: 100vh;

  .register-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .register-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      padding-left: 300px;
      padding-right: 300px;
      padding-bottom: 100px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
  }

  .register-footer {
    position: fixed;
    bottom: 10px;
    width: 100%;
    padding: 30px 50px;
    color: #000000d9;
    font-size: 18px;
    background: #fff;
    text-align: center;
  }

  // 头像上传
  .avatar-uploader .el-upload {
    border: 1px dashed #bd2525;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #4770a9;
    width: 136px;
    height: 136px;
    line-height: 136px;
    text-align: center;
  }

  .avatar {
    width: 136px;
    height: 136px;
    display: block;
  }
}
</style>
